package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.beans.ResultBean;
import com.offcn.beans.User;
import com.offcn.service.UserService;
import com.offcn.utils.PageTool;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    //提供一个UserService的属性
    private UserService userService = new UserService();
    // 提供一个ResultBean对象属性
    private ResultBean resultBean;

    //提供一个登录的方法
    protected void amdinLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String imageCode = req.getParameter("imageCode");

        // 获取session对象
        HttpSession session = req.getSession();
        String verCode = (String) session.getAttribute("verCode");
        // 先判断验证码是否正确
        if (imageCode.equalsIgnoreCase(verCode)) {
            //如果正确,再去根据用户名和密码查数据库
            boolean isSuc = userService.adminLogin(username, password, session);
            if (isSuc) {
                resultBean = new ResultBean(200, "登录成功", isSuc);
            } else {
                resultBean = new ResultBean(500, "用戶名或者密碼不正塙", isSuc);
            }
        } else {
            // 验证码不正确返回
            resultBean = new ResultBean(500, "验证码不正确", null);
        }
        // 结果转为JSON数据返回
        String data = JSON.toJSONString(resultBean);
        resp.getWriter().write(data);
    }

    /**
     * 查询当前用户信息的方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findCurrentUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取session存储用户对象
        HttpSession session = req.getSession();
        // 取出存储用户对象
        User user = (User) session.getAttribute("user");
        // 把结果封装到ResultBean中
        resultBean = new ResultBean(200, "查询当前用户信息", user);

        String data = JSON.toJSONString(resultBean);
        resp.getWriter().write(data);
    }

    /**
     * 登出的方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
    }

    /**
     * 查询所有用户信息的方法
     */

    protected void findAllUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取当前页码
        String currentPage = req.getParameter("currentPage");
        // 获取每页显示个数
        String pageSize = req.getParameter("pageSize");
        // 获取参数search
        String search = req.getParameter("search");
        // 获取总个数, 根据模糊查询获取总个数
        int totalCount = userService.findTotalCount(search);

        // 创建PageTool对象
        PageTool pageTool = new PageTool(totalCount, currentPage, Integer.parseInt(pageSize));
        // 利用分页数据查询 根据模糊查询当前页的数据
        List<User> users = userService.findAllUser(pageTool, search);
        // 把当前页的查询出的数据,赋值给分页的list属性.
        pageTool.setList(users);
        if (users == null || users.size() == 0) {
            resultBean = new ResultBean(500, "查询所有用户信息失败", pageTool);
        } else {
            resultBean = new ResultBean(200, "查询所有用户信息成功", pageTool);
        }//转jSON
        String data = JSON.toJSONString(resultBean);
        resp.getWriter().write(data);

    }

    /**
     * 添加用户的方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        // 获取参数
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        BeanUtils.populate(user, map);
        boolean isSuc = userService.addUser(user);
        if (isSuc) {
            resultBean = new ResultBean(200, "添加用户信息成功", isSuc);
        }else{
            resultBean = new ResultBean(500, "添加用户信息成功", isSuc);
        }
        String data = JSON.toJSONString(resultBean);
        resp.getWriter().write(data);
    }
}
